
/***************默认调用分页查询的函数***************/
$("#showAddDialog").click(function () {
    $("#addDialog").show()
})

$(".closeBt").click(function () {
    $("#addDialog").hide()
    $("#upDialog").hide()
})

$("#addBt").click(function (){
    let data = $("#addForm").serialize()
    $.ajax({
        type: 'post',
        url: '/stu',
        data: data,
        success: function (resData){
            if (resData.code === "200"){
                $("#addDialog").hide()
                getByPage(1,$(this).pageCount)
            }
        }
    })
})
//-----------------修改-----------------------
$(".middle").on("click","#upBt",function (){
    let data = $("#upForm1").serialize()
    console.log(data)
    $.ajax({
        type:'Put',
        url: "/stu",
        data: data,
        success: function (resData){
            if (resData.code === "200"){
                $("#upDialog").hide()
                getByPage(1,$(this).pageCount)
            }
        }
    })
})

getByPage()

function getByPage(pageNow= 1,pageCount= 5){
    /*--------------发起请求做分页查询---------------*/
    $.ajax({
        type: 'get',
        url: '/stu',
        data: {"pageNow":pageNow,"pageCount":pageCount},
        success: function (resData) {
            let pageBean = resData.data
            /*************************更新表格************************/
            let stuList = pageBean.data
            let htmlStr = ""
            for (let i = 0;i<stuList.length;i++){
                let stu = stuList[i]
                htmlStr += "<tr class='infoStu' style='text-align: center'>\n" +
                    "            <td>"+stu.id+"</td>\n" +
                    "            <td>"+stu.stuNumber+"</td>\n" +
                    "            <td>"+stu.stuName+"</td>\n" +
                    "            <td>"+stu.stuAge+"</td>\n" +
                    "            <td>"+stu.stuMajor+"</td>\n" +
                    "            <td><a class='update' lang='"+i+"' href='javascript:;'>修改</a>&nbsp;&nbsp;" +
                    "                <a class='delete' lang='"+stu.id+"' href='javascript:;'>删除</a></td>\n" +
                    "        </tr>"
            }
            $("#stuTable tbody").html(htmlStr)

            //-------------删除------------------
            $(".delete").click(function (){
                let id = $(this).attr("lang")
                alert("aaa")
                var r = confirm("真的要删除嘛?")
                if (r){
                    $.ajax({
                        type: 'delete',
                        url: '/stu/' + id,
                        success: function (resData){
                            if (resData.code === "200"){
                                getByPage(1,$("#pageCountSel").var)
                            }
                        }
                    })
                }
            })


            $(".update").click(function (){
                $("#upDialog").show()
                let id = $(this).attr("lang")
                let data = resData.data.data[id]
                let html = "<form id=\"upForm1\">\n" +
                    "            <div>ID:<input type=\"text\" name=\"ID\" value='"+ data.id +"' ></div>\n" +
                    "            <div>学号:<input type=\"text\" name=\"stuNumber\" value='"+ data.stuNumber +"' ></div>\n" +
                    "            <div>姓名:<input type=\"text\" name=\"stuName\" value='"+ data.stuName +"'></div>\n" +
                    "            <div>年龄:<input type=\"number\" name=\"stuAge\" value='"+ data.stuAge +"'></div>\n" +
                    "            <div>专业:\n" +
                    "                <select name=\"stuMajor\">\n" +
                    "                    <option>"+ data.stuMajor +"</option>\n" +
                    "                    <option>计算机科学与应用</option>\n" +
                    "                    <option>软件工程</option>\n" +
                    "                    <option>电子信息工程</option>\n" +
                    "                    <option>网络工程</option>\n" +
                    "                </select>\n" +
                    "            </div>\n" +
                    "            <div><input type=\"button\" id=\"upBt\"  value=\"确认\"></div>\n" +
                    "        </form>"
                $("#upDialog .middle").html(html)

            })

            /********************设置每页显示多少条记录的选中项************************/
            $("#pageCountSel").val(pageBean.pageCount)

            /******************改变每页显示的条数时重新查询******************/
            $("#pageCountSel").change(function () {
                getByPage(1,$(this).val())
            })

            /*******************更新页码********************/
            let totalPages = pageBean.totalPages
            htmlStr = ""
            for (let i = 1;i<=totalPages;i++){
                htmlStr += "<li><a class='pageBt "+(pageBean.pageNow===i?'now':'')+"' href='javascript:;'>"+i+"</a></li>"
            }
            $("#pageUl").html(htmlStr)

            /********************给页码添加点击监听**********************/
            $(".pageBt").click(function () {

                let pageNow = $(this).text()
                let pageCount = $("#pageCountSel").val()
                getByPage(pageNow,pageCount)
            })



        }
    })
}